package pl.register.android.activities.tasks;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.WindowManager;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import pl.register.android.R;
import pl.register.android.activities.tasks.builders.TaskEditorDialogBuilder;
import pl.register.android.task.Task;

import java.util.Date;

/**
 * @author Mateusz Strzelecki
 * @since 2013-05-02
 */
public class TasksActivity extends SherlockFragmentActivity implements TasksListFragment.OnItemSelectedListener {

    private TasksListFragment listFragment;

    private TaskDisplayFragment displayFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.tasks_layout);

        registerFragments();
        onCreateActionBar();
    }

    private void registerFragments() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        listFragment = (TasksListFragment) fragmentManager.findFragmentById(R.id.listFragment);
        displayFragment = (TaskDisplayFragment) fragmentManager.findFragmentById(R.id.displayFragment);
    }

    private void onCreateActionBar() {
        getSupportActionBar().setIcon(R.drawable.ic_tasks_dark);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.menu_tasks, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_tasks_add:
                showTaskEditorDialog(Task.nullObject());
                return true;
            case R.id.menu_tasks_delete:
                listFragment.showDeleteBtn();
                return true;
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTaskItemSelected(Task task) {
        displayFragment.settleDisplayTask(task);
    }

    @Override
    public void onTaskEditing(Task task) {
        showTaskEditorDialog(task);
    }

    @Override
    public void onTaskRemoved(Task task) {
        displayFragment.removedTask(task);
    }

    private void showTaskEditorDialog(final Task task) {
        final TaskEditorDialogBuilder taskEditorBuilder = new TaskEditorDialogBuilder(this, task);
        taskEditorBuilder.setNegativeButton(R.string.btn_name_cancel, null);
        taskEditorBuilder.setPositiveButton(R.string.btn_name_save, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                taskEditorBuilder.getValuesFromFields(task);
                if (task.isPersisted()) {
                    listFragment.saveTask(task);
                    displayFragment.refresh();
                } else {
                    listFragment.saveTask(task);
                    listFragment.addTask(task);
                }
            }
        });
        taskEditorBuilder.create().show();
    }
}